package com.bbz.core.Ali;

import java.util.*;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;

//评测题目: 编写如下程序：生成10个线程，每个线程拿到一个1-1000的随机正整数，任意两个线程不能拿到相同的数字，然后按数字从小到大的顺序依次让拿到相应数字的线程把自己的数字打印出来

public class Test {
    static TreeSet set = new TreeSet();
    static HashSet tempSet = new HashSet();
    static ArrayList arrayList = new ArrayList();

    public static void main(String args[]) {
        //初始化set
        getRandom();
        Process pro = new Process();
        ExecutorService e = Executors.newFixedThreadPool(10);

        int i = 0;
        for (Iterator iter = set.iterator(); iter.hasNext(); ) {
            TreeModel treeModel = (TreeModel) iter.next();
            System.out.println(treeModel.getRandom());
            Thread thread = new Thread(new Demo(pro, treeModel.getRandom(), "ThreadName--" + i));
            thread.start();
            try {
                thread.join();
            } catch (InterruptedException e1) {
                e1.printStackTrace();
            }
            i++;
        }
        e.shutdown();
    }


    public static void getRandom() {

        for (int i = 0; i < Integer.MAX_VALUE; i++) {
            int random = new Random().nextInt(1000) + 1;
            try {
                if(tempSet.size()==10){
                    break;
                }
                if (!tempSet.contains(random)) {
                    tempSet.add(random);
                    set.add(new TreeModel(random));
                }
            } catch (Exception e) {
                continue;
            }

        }
    }


}
